package service

import (
	"encoding/json"
	"fmt"
	"usplatformserver/internal/model"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/zmq"
)

// int为0值时，golang默认处理成null，这里OrganType改为*int类型
type UploadListRequest struct {
	Option    string  `form:"option" binding:"max=100"`
	BeginTime string  `form:"beginTime"`
	EndTime   string  `form:"endTime"`
	OrganType *uint32 `form:"organType"`
}

// 上传文件高亮请求
type UploadFileHighlightRequest struct {
	Option    string  `form:"option" binding:"max=100"`
	BeginTime string  `form:"beginTime"`
	EndTime   string  `form:"endTime"`
	FileId    *uint32 `form:"fileId" binding:"required"`
	PageSize  *int    `form:"pageSize" binding:"required"`
	OrganType *uint32 `form:"organType"`
}

// 发布视频分析任务请求
type pubAnalysisTaskMsgReq struct {
	MsgType uint32                `json:"msgType"`
	Data    *model.UploadFileInfo `json:"data"`
}

/**
 * @brief 插入数据
 * @param organType-检查类型
 * @param fileName-文件名
 * @param filePath-文件路径
 * @param fileSize-文件大小
 * @return 文件ID，错误信息
 */
func (svc *Service) InsertUploadFile(organType int, fileName, filePath, fileSize string) (int, error) {
	return svc.dao.InsertUploadFile(organType, fileName, filePath, fileSize)
}

func (svc *Service) DeleteUploadFile(fileId int) error {
	return svc.dao.DeleteUploadFile(fileId)
}

/**
 * @brief 获取上传文件列表
 * @param UploadListRequest-请求参数
 * @param pager-分页信息
 * @return 列表&错误信息
 */
func (svc *Service) GetUploadFileList(param *UploadListRequest, pager *app.Pager) ([]*model.UploadFileInfo, error) {
	//重组起始时间
	strBegin := param.BeginTime
	if strBegin != "" {
		strBegin += " 00:00:00"
	}

	//重组结束时间
	strEnd := param.EndTime
	if strEnd != "" {
		strEnd += " 23:59:59"
	}
	return svc.dao.GetUploadFileList(*param.OrganType, param.Option, strBegin, strEnd, pager.Page, pager.PageSize)
}

/**
 * @brief 获取上传文件列表总数
 * @param UploadListRequest-请求参数
 * @return 总数&错误信息
 */
func (svc *Service) CountUploadFile(param *UploadListRequest) (int, error) {
	//重组起始时间
	strBegin := param.BeginTime
	if strBegin != "" {
		strBegin += " 00:00:00"
	}

	//重组结束时间
	strEnd := param.EndTime
	if strEnd != "" {
		strEnd += " 23:59:59"
	}
	return svc.dao.CountUploadFile(*param.OrganType, param.Option, strBegin, strEnd)
}

/**
 * @brief 上传文件是否存在
 * @param fileName-文件名
 * @return 错误信息
 */
func (svc *Service) IsExistUploadFile(fileName string) (bool, error) {
	return svc.dao.IsExistFile(fileName)
}

func (svc *Service) PubAnalysisTask(addr string, id int, fileName, fileFullName string) {
	fmt.Println("file id: ", id, " file path: ", fileFullName)

	uploadInfo := model.UploadFileInfo{
		FileName: fileName,
		FilePath: fileFullName,
	}
	uploadInfo.Model = &model.Model{ID: uint32(id)}

	//重组zmq消息
	msgReq := pubAnalysisTaskMsgReq{
		MsgType: zmq.MsgZmqPubVideoAnalysisTask.MsgType,
		Data:    &uploadInfo,
	}

	msgReqStr, err := json.Marshal(msgReq)
	if err != nil {
		fmt.Println("json.Marshal(pub analysis task msg req) failed: ", err)
		return
	}
	//发送分析视频消息
	_, err = zmq.SendMessage(addr, string(msgReqStr))
	if err != nil {
		fmt.Println("zmq.SendMessage failed: ", err)
	}
}

func (svc *Service) PubAnalysisOfflineImagesTask(addr string, organType, id int, fileName, fileFullName string) error {
	fmt.Println("organType: ", organType, "file id: ", id, " file path: ", fileFullName)
	uploadInfo := model.UploadFileInfo{
		FileName:  fileName,
		FilePath:  fileFullName,
		OrganType: organType,
	}
	uploadInfo.Model = &model.Model{ID: uint32(id)}

	//重组zmq消息
	msgReq := pubAnalysisTaskMsgReq{
		MsgType: zmq.MsgZmqPubImagesAnalysisTask.MsgType,
		Data:    &uploadInfo,
	}

	msgReqStr, err := json.Marshal(msgReq)
	if err != nil {
		fmt.Println("json.Marshal(pub analysis task msg req) failed: ", err)
		return err
	}
	//发送分析视频消息
	_, err = zmq.SendMessage(addr, string(msgReqStr))
	if err != nil {
		fmt.Println("zmq.SendMessage failed: ", err)
		return err
	}
	return nil
}
